Linux服务器上配置Nginx开机自启动

您所在的位置:网站首页 linux nginx服务启动 Linux服务器上配置Nginx开机自启动

Linux服务器上配置Nginx开机自启动

2023-07-04 00:57| 来源: 网络整理| 查看: 265

文章目录 前言方法一、编辑/etc/rc.local,添加开机启动运行命令;方法二、添加/etc/init.d/nginx,通过chkconfig配置开机启动服务;1.添加脚本2.脚本内容3.授权4. 使用chkconfig进行管理5. 配置完成

前言

由于服务器是部署到机房的,所以当机房检修或者其他意外情况就会需求重新启动项目

方法一、编辑/etc/rc.local,添加开机启动运行命令;

这里如果是使用vim命令,不明白的可以参考 菜鸟教程

# 直接编辑/etc/rc.local文件,文件内容最底下添加启动命令 [root@appser ~]# vim /etc/rc.local # 最后追加内容 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf & 方法二、添加/etc/init.d/nginx,通过chkconfig配置开机启动服务; 1.添加脚本

这里有需要注意的地方,这里最好是使用vim去编辑,我自己是用MobaXterm 这个工具,直接编辑的文件,但是最后会报错,会有文件格式问题. 使用 MobaXterm 工具 路径是 /etc/rc.d/init.d/ 参考文章: env: /etc/init.d/nginx: 没有那个文件或目录错误的解决方式

# 添加/etc/init.d/nginx脚本,通过chkconfig配置开机启动服务 [root@appser ~]# touch /etc/init.d/nginxd [root@appser ~]# vim /etc/init.d/nginxd 2.脚本内容

nginxd文件内容具体如下: 需要修改的位置是: nginxd #改成自己的安装路径

nginx_config #改成自己的安装路径

nginx_pid #改成自己的

#!/bin/bash # chkconfig: 2345 85 15 # description: nginx Startup script for the Nginx HTTP Server # processname: nginx nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL

也可以使用官方提供的 启动脚本

3.授权

给脚本文件赋予相关的执行权限:

// (a+x参数表示 ==> all user can execute 所有用户可执行) chmod a+x /etc/init.d/nginxd // 或者是 755 权限 chmod 755 /etc/init.d/nginxd 4. 使用chkconfig进行管理 chkconfig --add /etc/init.d/nginxd #将nginx服务加入chkconfig管理列表 chkconfig nginxd on #设置开机运行该服务 5. 配置完成 [root@appser init.d]# service nginxd start Starting nginxd (via systemctl): [ OK ] 至此,即配置完成,可通过chkconfig命令查看系统服务运行状态。 [root@appser init.d]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. redisd 0:off 1:off 2:on 3:on 4:on 5:on 6:off nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3